<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java学习笔记-IO | ShenShilei's Blog</title><meta name="keywords" content="IO"><meta name="author" content="Shen Shilei"><meta name="copyright" content="Shen Shilei"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="学习Java的IO流。">
<meta property="og:type" content="article">
<meta property="og:title" content="Java学习笔记-IO">
<meta property="og:url" content="https://blog.shenshilei.site/post/d7ed/index.html">
<meta property="og:site_name" content="ShenShilei&#39;s Blog">
<meta property="og:description" content="学习Java的IO流。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/SunlitScree_ZH-CN7556627842_1920x1080.jpg">
<meta property="article:published_time" content="2020-03-02T01:32:50.000Z">
<meta property="article:modified_time" content="2020-07-05T12:15:36.000Z">
<meta property="article:author" content="Shen Shilei">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="IO">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/SunlitScree_ZH-CN7556627842_1920x1080.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://blog.shenshilei.site/post/d7ed/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":180,"position":"top","messagePrev":"这是一篇创建于","messageNext":"天前的文章，其中的信息可能已经有所发展或是发生改变。"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Shen Shilei","link":"链接: ","source":"来源: ShenShilei's Blog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-05 20:15:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }})()</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/kang-static/Hexo/css/iconfont.min.css"><style type="text/css">.aplayer.aplayer-narrow .aplayer-body{left: -66px !important;transition: left 1s;} .aplayer.aplayer-narrow .aplayer-body:hover{left: 0 !important;}</style><style type="text/css">body{cursor: url(https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/image/cursor.ico),default}</style><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@1.6/img/custom/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">60</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk/"><i class="fa-fw fa fa-comment"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page" href="/analytics/"><i class="fa-fw fas fa-chart-line"></i><span> 统计</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/bangumis/"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具箱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/box/"><i class="fa-fw fas fa-th-large"></i><span> 网站聚合</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@2.0/"><i class="fa-fw fa fa-hdd"></i><span> MyCDN</span></a></li><li><a class="site-page" href="/note/"><i class="fa-fw fas fa-business-time"></i><span> 便利贴</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://music.shenshilei.site/"><i class="fa-fw fas fa-music"></i><span> 音乐播放器</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://webssh.shenshilei.site/"><i class="fa-fw fas fa-terminal"></i><span> SSH在线连接</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-pencil-alt"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-paperclip"></i><span> 关于</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/"><i class="fa-fw fas fa-rocket"></i><span> 我</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://shenshilei1022.avosapps.us/"><i class="fa-fw fas fa-list-alt"></i><span> 评论管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="http://bt.shenshilei.site:8888/shenshilei"><i class="fa-fw fas fa-window-maximize"></i><span> 服务器管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://console.leancloud.app/applist.html#/apps"><i class="fa-fw fas fa-window-restore"></i><span> leancloud应用</span></a></li></ul></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/SunlitScree_ZH-CN7556627842_1920x1080.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">ShenShilei's Blog</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk/"><i class="fa-fw fa fa-comment"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page" href="/analytics/"><i class="fa-fw fas fa-chart-line"></i><span> 统计</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/bangumis/"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具箱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/box/"><i class="fa-fw fas fa-th-large"></i><span> 网站聚合</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@2.0/"><i class="fa-fw fa fa-hdd"></i><span> MyCDN</span></a></li><li><a class="site-page" href="/note/"><i class="fa-fw fas fa-business-time"></i><span> 便利贴</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://music.shenshilei.site/"><i class="fa-fw fas fa-music"></i><span> 音乐播放器</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://webssh.shenshilei.site/"><i class="fa-fw fas fa-terminal"></i><span> SSH在线连接</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-pencil-alt"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-paperclip"></i><span> 关于</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/"><i class="fa-fw fas fa-rocket"></i><span> 我</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://shenshilei1022.avosapps.us/"><i class="fa-fw fas fa-list-alt"></i><span> 评论管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="http://bt.shenshilei.site:8888/shenshilei"><i class="fa-fw fas fa-window-maximize"></i><span> 服务器管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://console.leancloud.app/applist.html#/apps"><i class="fa-fw fas fa-window-restore"></i><span> leancloud应用</span></a></li></ul></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">Java学习笔记-IO</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-03-02T01:32:50.000Z" title="发表于 2020-03-02 09:32:50">2020-03-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-07-05T12:15:36.000Z" title="更新于 2020-07-05 20:15:36">2020-07-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/%E5%9F%BA%E7%A1%80/">基础</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>20分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/post/d7ed/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count comment-count" data-xid="/post/d7ed/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-I-O-文件对象"><a href="#1-I-O-文件对象" class="headerlink" title="1. I/O 文件对象"></a>1. I/O 文件对象</h2><p>文件和文件夹都是用File代表。</p>
<p>使用File类根据绝对路径或相对路径创建File对象。</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>简介</th>
</tr>
</thead>
<tbody>
<tr>
<td>exists</td>
<td>文件是否存在</td>
</tr>
<tr>
<td>isDirectory</td>
<td>是否是文件夹</td>
</tr>
<tr>
<td>isFile</td>
<td>是否是文件</td>
</tr>
<tr>
<td>length</td>
<td>文件长度</td>
</tr>
<tr>
<td>lastModified</td>
<td>文件最后修改时间</td>
</tr>
<tr>
<td>setLastModified</td>
<td>设置文件修改时间</td>
</tr>
<tr>
<td>renameTo</td>
<td>重命名</td>
</tr>
<tr>
<td>list</td>
<td>以字符串数组的形式，返回当前文件夹下所有文件</td>
</tr>
<tr>
<td>listFiles</td>
<td>以文件数组的形式，返回当前文件夹下所有文件（可以返回文件夹，不能返回子文件夹下的文件）</td>
</tr>
<tr>
<td>getParent</td>
<td>以字符串形式返回获取所在文件夹</td>
</tr>
<tr>
<td>getParentFile</td>
<td>以文件形式返回获取所在文件夹</td>
</tr>
<tr>
<td>mkdir</td>
<td>创建文件夹，如果父文件夹不存在，创建无效</td>
</tr>
<tr>
<td>mkdirs</td>
<td>创建文件夹，如果父文件夹不存在，会创建父文件夹</td>
</tr>
<tr>
<td>createNewFile</td>
<td>创建一个空文件，如果父文件夹不存在，会抛出异常</td>
</tr>
<tr>
<td>listRoots</td>
<td>列出所有盘符</td>
</tr>
<tr>
<td>delete</td>
<td>删除文件</td>
</tr>
<tr>
<td>deleteOnExit</td>
<td>JVM结束的时候，删除文件，常用于临时文件的删除。</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">package io.demo01;</span><br><span class="line"></span><br><span class="line">import java.io.File;</span><br><span class="line"></span><br><span class="line">public class Practice02 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        // 遍历文件夹下的所有内容，包括子文件夹。</span><br><span class="line">        File file = new File(&quot;D:\\Media&quot;);</span><br><span class="line">        goThroughDirectory(file);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void goThroughDirectory(File file) &#123;</span><br><span class="line">        if (file.isDirectory()) &#123;</span><br><span class="line">            File[] files = file.listFiles();</span><br><span class="line">            for (File file1 : files) &#123;</span><br><span class="line">                goThroughDirectory(file1);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            System.out.printf(&quot;文件路径：%s，文件大小：%d&quot;, file.getAbsolutePath(), file.length());</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-什么是流？"><a href="#2-什么是流？" class="headerlink" title="2. 什么是流？"></a>2. 什么是流？</h2><p>什么是流(Stream)，流就是一系列的数据。当不同的介质之间有数据交互的时候，Java就使用流来实现。数据源可以是文件，还可以是数据库，网络甚至是其它的程序。</p>
<p>比如读取文件的数据到程序中，站在程序的角度来看，就叫做输入流。</p>
<p>输入流：InputStream<br>输出流：OutputStream</p>
<h2 id="3-字节流"><a href="#3-字节流" class="headerlink" title="3. 字节流"></a>3. 字节流</h2><p>InputStream 字节输入流<br>OutputStream 字节输出流<br>用于以字节的形式读取和写入数据。</p>
<h3 id="3-1-ASCII码"><a href="#3-1-ASCII码" class="headerlink" title="3.1 ASCII码"></a>3.1 ASCII码</h3><p>所有的数据存放在计算机中都是以数字的形式存放的。所以字母就需要转换为数字才能够存放。比如A就对应的数字65，a对应的数字97。不同的字母和符号对应不同的数字，就是一张码表。ASCII就是这样的一张码表。它只包含简单的英文字母，符号，数字等等。不包含中文，德文，俄语等复杂的。</p>
<h3 id="3-2-以字节流的形式读取文件内容"><a href="#3-2-以字节流的形式读取文件内容" class="headerlink" title="3.2 以字节流的形式读取文件内容"></a>3.2 以字节流的形式读取文件内容</h3><p>InputStream是字节输入流，同时也是抽象类，只提供方法声明，不提供方法的具体实现。FileInputStream是InputStream的子类，以FileInputStream进行文件读取。</p>
<p>步骤：</p>
<ol>
<li>创建File对象</li>
<li>创建基于文件的字节输入流</li>
<li>创建字节数组（长度就是文件长度）</li>
<li>以字节流的形式读取文件所有内容</li>
<li>使用完流，应该进行关闭</li>
</ol>
<p>写入文件内容类似（FileOutputStream）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">package io.demo03;</span><br><span class="line"></span><br><span class="line">import java.io.File;</span><br><span class="line">import java.io.FileOutputStream;</span><br><span class="line">import java.io.IOException;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        /* 当文件不存在时候，是会自动创建文件的。</span><br><span class="line">           但如果路径当中有文件夹存在时，而文件夹又没有存在时，则会抛出异常。</span><br><span class="line">           怎么自动创建目录呢？</span><br><span class="line"></span><br><span class="line">           判断该文件对象是否存在，若不存在，则getParentFile对象，创建父目录(mkdirs)</span><br><span class="line">         */</span><br><span class="line">        String path = &quot;D:\\def\\abc\\lol.txt&quot;;</span><br><span class="line">        writeFile(path);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void writeFile(String path) &#123;</span><br><span class="line">        // 1. 创建File对象</span><br><span class="line">        File f = new File(path);</span><br><span class="line">        if (!f.exists()) &#123;</span><br><span class="line">            System.out.println(f.getParentFile().mkdirs());</span><br><span class="line">        &#125;</span><br><span class="line">        // 2. 创建字节数组</span><br><span class="line">        byte[] bytes = &#123;78, 71, 95&#125;;</span><br><span class="line">        try &#123;</span><br><span class="line">            // 3. 创建字节输出流</span><br><span class="line">            FileOutputStream fos = new FileOutputStream(f);</span><br><span class="line">            // 4. 把数据写入到流中</span><br><span class="line">            fos.write(bytes);</span><br><span class="line">            // 5. 关闭输出流</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">package io.demo03;</span><br><span class="line"></span><br><span class="line">import java.io.File;</span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.FileOutputStream;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.Arrays;</span><br><span class="line"></span><br><span class="line">public class Practice02 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) throws IOException&#123;</span><br><span class="line">        /*</span><br><span class="line">        找到一个大于100k的文件，按照100k为单位，拆分成多个子文件，并且以编号作为文件名结束</span><br><span class="line">         */</span><br><span class="line">        splitFile(&quot;D:\\JDK_API_1_6_zh_CN.chw&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void splitFile(String path) throws IOException &#123;</span><br><span class="line">        File f = new File(path);</span><br><span class="line">        FileInputStream fis = new FileInputStream(f);</span><br><span class="line">        // 创建字节数组</span><br><span class="line">        byte[] bytes = new byte[(int) f.length()];</span><br><span class="line">        // 读取文件</span><br><span class="line">        fis.read(bytes);</span><br><span class="line">        fis.close();</span><br><span class="line"></span><br><span class="line">        int size = 100 * 1024;</span><br><span class="line">        // 判断要分割成几个文件</span><br><span class="line">        int sum;</span><br><span class="line">        if (0 == bytes.length % size) &#123;</span><br><span class="line">            sum = (int) (bytes.length / size);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            sum = (int) (bytes.length / size) + 1;</span><br><span class="line">        &#125;</span><br><span class="line">        for (int i = 0; i &lt; sum; i++) &#123;</span><br><span class="line">            String fileName = String.format(f.getName() + &quot;-%d&quot;, i + 1);</span><br><span class="line">            File fileSplit = new File(fileName);</span><br><span class="line">            FileOutputStream fos = new FileOutputStream(fileSplit);</span><br><span class="line">            byte[] eachContent;</span><br><span class="line">            if (i + 1 &lt; sum) &#123;</span><br><span class="line">                // 不是最后一个分割文件</span><br><span class="line">                eachContent = Arrays.copyOfRange(bytes, i * size, (i + 1) * size);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                // 最后一个分割文件</span><br><span class="line">                eachContent = Arrays.copyOfRange(bytes, i*size, bytes.length);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            fos.write(eachContent);</span><br><span class="line">            fos.close();</span><br><span class="line"></span><br><span class="line">            System.out.format(&quot;输出子文件：%s，大小是%d字节&quot;,</span><br><span class="line">                    fileSplit.getName(), fileSplit.length());</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">package io.demo03;</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice03 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) throws IOException&#123;</span><br><span class="line">        // 把上述拆分出来的文件，合并成一个原文件。</span><br><span class="line">        mergeFile(4, &quot;JDK_API_1_6_zh_CN.chw&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void mergeFile(int sum, String fileName) throws IOException &#123;</span><br><span class="line">        File linkFile = new File(fileName);</span><br><span class="line">        FileOutputStream fos = new FileOutputStream(linkFile);</span><br><span class="line"></span><br><span class="line">        for (int i = 0; i &lt; sum; i++) &#123;</span><br><span class="line">            String subFile = String.format(fileName + &quot;-%d&quot;, i + 1);</span><br><span class="line">            File f = new File(subFile);</span><br><span class="line">            FileInputStream fis = new FileInputStream(f);</span><br><span class="line">            byte[] bytes = new byte[(int) f.length()];</span><br><span class="line">            fis.read(bytes);</span><br><span class="line">            fis.close();</span><br><span class="line"></span><br><span class="line">            fos.write(bytes);</span><br><span class="line">            fos.flush(); // 刷新释放输出流数据</span><br><span class="line">            System.out.printf(&quot;把子文件%s合并到%s&quot;, subFile, fileName);</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">        fos.close();</span><br><span class="line"></span><br><span class="line">        System.out.printf(&quot;合并完成，文件大小为：%d字节&quot;, linkFile.length());</span><br><span class="line">        System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-关闭流的方式"><a href="#4-关闭流的方式" class="headerlink" title="4. 关闭流的方式"></a>4. 关闭流的方式</h2><p>所有的流，无论是输入流还是输出流，使用完毕之后，都应该关闭。如果不关闭，会产生对资源占用的浪费。当量比较大的时候，会影响到业务的正常开展。</p>
<h3 id="4-1-在try中关闭"><a href="#4-1-在try中关闭" class="headerlink" title="4.1 在try中关闭"></a>4.1 在try中关闭</h3><p>在try的作用域中关闭文件输入流。</p>
<p>弊端：如果文件不存在，或者读取的时候出现问题抛出异常，那么就没有执行关闭流的代码，存在巨大的资源占用隐患。<strong>不推荐使用</strong></p>
<h3 id="4-2-在finally中关闭"><a href="#4-2-在finally中关闭" class="headerlink" title="4.2 在finally中关闭"></a>4.2 在finally中关闭</h3><p>这是标准的关闭流的方式</p>
<ol>
<li>首先把流的引用声明在try外面，如果声明在try里面，其作用域无法抵达finally</li>
<li>在finally关闭之前，需要先判断流的引用是否为空</li>
<li>关闭的时候，需要再进行一次try catch处理。</li>
</ol>
<p>这是标准的严谨的关闭流的方式，但是比较繁琐，所以写不重要的代码时，一般采用有隐患的try方式，比较简单。</p>
<h3 id="4-3-使用try-的方式"><a href="#4-3-使用try-的方式" class="headerlink" title="4.3 使用try()的方式"></a>4.3 使用try()的方式</h3><p>把流定义再try()里，try, catch或者finally结束的时候，会自动关闭，这种编写代码的方式叫做try-with-resources，这是从JDK7开始支持的技术。</p>
<h2 id="5-字符流"><a href="#5-字符流" class="headerlink" title="5. 字符流"></a>5. 字符流</h2><p>Reader 字符输入流<br>Writer 字符输出流</p>
<p>专门用于字符的形式读取和写入数据</p>
<h3 id="5-1-使用字符流读取文件"><a href="#5-1-使用字符流读取文件" class="headerlink" title="5.1 使用字符流读取文件"></a>5.1 使用字符流读取文件</h3><p>FileReader是Reader的子类。</p>
<p>步骤：</p>
<ol>
<li>创建File对象</li>
<li>创建基于File对象的Reader</li>
<li>创建字符数组，其长度就是文件的长度</li>
<li>以字符流的形式读取文件所有内容。</li>
</ol>
<h3 id="5-2-使用字符流把字符串写入到文件"><a href="#5-2-使用字符流把字符串写入到文件" class="headerlink" title="5.2 使用字符流把字符串写入到文件"></a>5.2 使用字符流把字符串写入到文件</h3><p>同读取文件类似。</p>
<h2 id="6-中文问题"><a href="#6-中文问题" class="headerlink" title="6. 中文问题"></a>6. 中文问题</h2><h3 id="6-1-编码问题"><a href="#6-1-编码问题" class="headerlink" title="6.1 编码问题"></a>6.1 编码问题</h3><p>计算机存放数据只能存储数字，所有的字符都会被转换为数字。</p>
<p>ASCII - 数字和西欧字母<br>GBK GB2312 BIG5 - 中文<br>UNICODE - 统一码，万国码</p>
<p>GB2312 是简体中文，BIG5是繁体中文，GBK同时包含简体和繁体以及日文。<br>UNICODE 包括了所有的文字，无论中文，英文，藏文，法文，世界所有的文字都包含其中</p>
<h3 id="6-2-UNICODE和UTF"><a href="#6-2-UNICODE和UTF" class="headerlink" title="6.2 UNICODE和UTF"></a>6.2 UNICODE和UTF</h3><p>由于UNICODE要存放所有的数据，每个数字对应4个字节，这样存储数据就会有很大的浪费。比如一篇文章大部分都是英文字母，按照UNICODE编码就会消耗很多存储空间。</p>
<p>在这种情况下，就出现了UNICODE的各种减肥子编码，比如UTF-8对数字和字母就使用一个字节，而对汉字就使用3个字节，从而达到了减肥还能保证健康的效果。</p>
<p><strong>Java采用的是UNICODE.</strong></p>
<h2 id="7-缓存流"><a href="#7-缓存流" class="headerlink" title="7. 缓存流"></a>7. 缓存流</h2><p>以介质是硬盘为例，字节流和字符流的弊端是：在每一次读写的时候，都会访问硬盘。如果读写的频率比较高的时候，其性能表现不佳。</p>
<p>为了解决以上弊端，采用缓存流。缓存流在读取的时候，会一次性读较多的数据到缓存中，以后每一次的读取，都是在缓存中访问，知道缓存中的数据读取完毕，再到硬盘中读取。</p>
<p>就好比吃饭，不同缓存就是每吃一口饭都到锅里去铲；用缓存就是先把饭盛到碗里，碗里的吃完了，再到锅里去铲。</p>
<p>缓存流在写入数据时，会先把数据写入到缓存区，知道缓存区达到一定的量，才把这些数据，一起写入到硬盘中去。按照这种模式，就不会像字节流，字符流那样每写一个字节都访问硬盘，从而减少了IO操作。</p>
<h3 id="7-1-使用缓存流读取数据"><a href="#7-1-使用缓存流读取数据" class="headerlink" title="7.1 使用缓存流读取数据"></a>7.1 使用缓存流读取数据</h3><p>缓存字符输入流BufferedReader可以一次读取一行数据。</p>
<p>步骤：</p>
<ol>
<li>创建File对象</li>
<li>创建文件字符流FileReader</li>
<li>缓存流必须建立在一个存在的流的基础上BufferedReader</li>
<li>一次读取一行内容。(readLine)</li>
</ol>
<h3 id="7-2-使用缓存流写出数据"><a href="#7-2-使用缓存流写出数据" class="headerlink" title="7.2 使用缓存流写出数据"></a>7.2 使用缓存流写出数据</h3><p>缓存字符输出流PrintWriter可以一次写出一行数据。</p>
<p>类似7.1缓存流读取数据。(println)</p>
<p><strong>会以覆盖的形式写入。</strong></p>
<h3 id="7-3-flush"><a href="#7-3-flush" class="headerlink" title="7.3 flush"></a>7.3 flush</h3><p>有的时候，需要立即把数据写入到硬盘，而不是等缓存满了才写出去，这时候就需要用到flush。(强制把缓存中的数据写入硬盘，无论缓存是否已满。)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">package io.demo07;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">设计一个方法，用于移除Java文件中的注释（以//开头的注释行）</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        File javaFile = new File(&quot;D:\\新建文本文档.java&quot;);</span><br><span class="line">        removeComments(javaFile);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void removeComments(File javaFile) &#123;</span><br><span class="line">        StringBuffer stringBuffer = new StringBuffer(&quot;&quot;);</span><br><span class="line">        try (FileReader fr = new FileReader(javaFile);</span><br><span class="line">             BufferedReader br = new BufferedReader(fr)) &#123;</span><br><span class="line">            while (true) &#123;</span><br><span class="line">                String line = br.readLine();</span><br><span class="line">                if (null == line) &#123;</span><br><span class="line">                    break;</span><br><span class="line">                &#125;</span><br><span class="line">                if (line.startsWith(&quot;//&quot;)) &#123;</span><br><span class="line">                    continue;</span><br><span class="line">                &#125;</span><br><span class="line">                stringBuffer.append(line);</span><br><span class="line">                stringBuffer.append(&quot;\n&quot;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        try (FileWriter fw = new FileWriter(javaFile);</span><br><span class="line">             PrintWriter pw = new PrintWriter(fw)) &#123;</span><br><span class="line">            pw.println(stringBuffer.toString());</span><br><span class="line">            pw.flush();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(&quot;修改成功&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="8-数据流"><a href="#8-数据流" class="headerlink" title="8. 数据流"></a>8. 数据流</h2><p>DataInputStream 数据输入流<br>DataOutputStream 数据输出流</p>
<p>使用数据流的writeUTF()和readUTF()可以进行数据的格式化顺序读写。例如可以通过DataOutputStream向文件顺序写入布尔值，整数和字符串。然后再通过DataInputStream顺序读入这些数据。</p>
<p><strong>要使用DataInputStream读取一个文件，这个文件必须是由DataOutputStream写出的，否则会报错。</strong></p>
<p><strong>数据流也要建立在其它流之上(字节流)</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">package io.demo08;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">要求：</span><br><span class="line">    第一种方式：使用缓存流把两个数字以字符串的形式写到文件里，再用缓存流以字符串的形式读取，最后转换为</span><br><span class="line">               两个数字。</span><br><span class="line">    第二种方式：使用数据流DataOutputStream向文件连续写入两个数字，然后用DataInputStream连续读取两个数字。</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        File file = new File(&quot;D:\\a.txt&quot;);</span><br><span class="line">        String str = &quot;234@78&quot;;</span><br><span class="line">        writeBuffer(str, file);</span><br><span class="line">        readBuffer(file);</span><br><span class="line"></span><br><span class="line">        File file2 = new File(&quot;D:\\b.txt&quot;);</span><br><span class="line">        int[] ints = &#123;234, 78&#125;;</span><br><span class="line">        writeDataStream(ints, file2);</span><br><span class="line">        readDataStream(file2);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void writeBuffer(String str, File file) &#123;</span><br><span class="line">        try (FileWriter fw = new FileWriter(file);</span><br><span class="line">             PrintWriter pw = new PrintWriter(fw)) &#123;</span><br><span class="line">            pw.println(str);</span><br><span class="line">            pw.flush();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void readBuffer(File file) &#123;</span><br><span class="line">        try (FileReader fr = new FileReader(file);</span><br><span class="line">             BufferedReader br = new BufferedReader(fr)) &#123;</span><br><span class="line">            String str;</span><br><span class="line">            int a = 0;</span><br><span class="line">            int b = 0;</span><br><span class="line">            while (null != (str = br.readLine())) &#123;</span><br><span class="line">                String[] strings = str.split(&quot;@&quot;);</span><br><span class="line">                a = Integer.parseInt(strings[0]);</span><br><span class="line">                b = Integer.parseInt(strings[1]);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(a);</span><br><span class="line">            System.out.println(b);</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void writeDataStream(int[] ints, File file) &#123;</span><br><span class="line">        try (FileOutputStream fos = new FileOutputStream(file);</span><br><span class="line">             DataOutputStream dos = new DataOutputStream(fos)) &#123;</span><br><span class="line">            for (int anInt : ints) &#123;</span><br><span class="line">                dos.writeInt(anInt);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void readDataStream(File file) &#123;</span><br><span class="line">        try (FileInputStream fis = new FileInputStream(file);</span><br><span class="line">             DataInputStream dis = new DataInputStream(fis)) &#123;</span><br><span class="line">            for (int i = 0; i &lt; 2; i++) &#123;</span><br><span class="line">                System.out.println(dis.readInt());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="9-对象流"><a href="#9-对象流" class="headerlink" title="9. 对象流"></a>9. 对象流</h2><p>对象流指的是可以直接把一个对象以流的方式传输给其它介质，比如硬盘。一个对象以流的形式进行传输，叫做<strong>序列化</strong>，该对象所对应的类，必须是实现Serializable接口</p>
<p><strong>对象流也要建立在其它流之上(字节流)</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">package io.demo09;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">准备一个长度为10，类型是Hero的数组，使用10个Hero对象初始化该数组</span><br><span class="line">然后把该数组序列化到一个文件heroes.lol</span><br><span class="line">接着使用ObjectInputStream读取该文件，并转换为Hero数组，验证该数组中的内容，是否和序列化之前一样。</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Hero[] heroes = new Hero[10];</span><br><span class="line">        for (int i = 0; i &lt; 10; i++) &#123;</span><br><span class="line">            heroes[i] = new Hero();</span><br><span class="line">        &#125;</span><br><span class="line">        File file = new File(&quot;D:\\heroes.lol&quot;);</span><br><span class="line">        writeObjectStream(heroes, file);</span><br><span class="line">        Hero[] heroes1 = readObjectStream(file);</span><br><span class="line">        System.out.println(heroes == heroes1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void writeObjectStream(Hero[] heroes, File file) &#123;</span><br><span class="line">        try (FileOutputStream fos = new FileOutputStream(file);</span><br><span class="line">             ObjectOutputStream oos = new ObjectOutputStream(fos)) &#123;</span><br><span class="line">            for (Hero hero : heroes) &#123;</span><br><span class="line">                oos.writeObject(hero);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static Hero[] readObjectStream(File file) &#123;</span><br><span class="line">        Hero[] heroes = new Hero[10];</span><br><span class="line">        try (FileInputStream fis = new FileInputStream(file);</span><br><span class="line">             ObjectInputStream ois = new ObjectInputStream(fis)) &#123;</span><br><span class="line">            int i = 0;</span><br><span class="line">            Hero o;</span><br><span class="line">            while (true) &#123;</span><br><span class="line">                if (null == (o = (Hero) ois.readObject())) &#123;</span><br><span class="line">                    break;</span><br><span class="line">                &#125;</span><br><span class="line">                heroes[i] = o;</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; catch (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        return heroes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="10-System-in"><a href="#10-System-in" class="headerlink" title="10. System.in"></a>10. System.in</h2><p>属于输入流InputStream的子类，可以从控制台输入数据。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package io;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        try (InputStream is = System.in) &#123;</span><br><span class="line">            int i = is.read();</span><br><span class="line">            System.out.println(i);</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="10-1-Scanner读取字符串"><a href="#10-1-Scanner读取字符串" class="headerlink" title="10.1 Scanner读取字符串"></a>10.1 Scanner读取字符串</h3><p>使用System.in.read虽然可以读取数据，但是很不方便<br>使用Scanner就可以逐行读取了</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">package io;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">自动创建一个有一个类属性的类文件。</span><br><span class="line">通过控制台，获取类名，属性名称，属性类型，根据一个模板文件，自动创建这个类文件，并为属性提供setter和getter</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line">import java.util.Scanner;</span><br><span class="line"></span><br><span class="line">public class Practice02 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line"></span><br><span class="line">        Scanner sc = new Scanner(System.in);</span><br><span class="line">        System.out.println(&quot;请输入类的名称：&quot;);</span><br><span class="line">        String className = sc.next();</span><br><span class="line">        System.out.println(&quot;请输入属性的类型：&quot;);</span><br><span class="line">        String type = sc.next();</span><br><span class="line">        System.out.println(&quot;请输入属性的名称：&quot;);</span><br><span class="line">        String property = sc.next();</span><br><span class="line"></span><br><span class="line">        File template = new File(&quot;D:\\template.txt&quot;);</span><br><span class="line">        createClass(className, property, type, template);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void createClass(String className, String property, String type, File template) &#123;</span><br><span class="line">        File targetFile = new File(String.format(&quot;D:\\%s.java&quot;, className));</span><br><span class="line"></span><br><span class="line">        // 将property的首字母变为大写Uproperty</span><br><span class="line">        char[] chars = property.toCharArray();</span><br><span class="line">        chars[0] = Character.toUpperCase(chars[0]);</span><br><span class="line">        String uProperty = new String(chars);</span><br><span class="line"></span><br><span class="line">        try (//使用缓冲流</span><br><span class="line">             BufferedReader br = new BufferedReader(new FileReader(template));</span><br><span class="line">             PrintWriter pw = new PrintWriter(new FileWriter(targetFile))) &#123;</span><br><span class="line">            String line;</span><br><span class="line">            while (null != (line = br.readLine())) &#123;</span><br><span class="line">                if (line.contains(&quot;@class@&quot;)) &#123;</span><br><span class="line">                    line = line.replace(&quot;@class@&quot;, className);</span><br><span class="line">                &#125;</span><br><span class="line">                if (line.contains(&quot;@property@&quot;)) &#123;</span><br><span class="line">                    line = line.replace(&quot;@property@&quot;, property);</span><br><span class="line">                &#125;</span><br><span class="line">                if (line.contains(&quot;@type@&quot;)) &#123;</span><br><span class="line">                    line = line.replace(&quot;@type@&quot;, type);</span><br><span class="line">                &#125;</span><br><span class="line">                if (line.contains(&quot;@Uproperty@&quot;)) &#123;</span><br><span class="line">                    line = line.replace(&quot;@Uproperty@&quot;, uProperty);</span><br><span class="line">                &#125;</span><br><span class="line">                pw.println(line);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            System.out.println(&quot;文件保存在：&quot; + targetFile.getAbsolutePath());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>template文件<br><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class @class@ &#123;</span><br><span class="line">    public @type@ @property@;</span><br><span class="line">    public @class@() &#123;</span><br><span class="line">    &#125;</span><br><span class="line">    public void set@Uproperty@(@type@  @property@)&#123;</span><br><span class="line">        this.@property@ = @property@;</span><br><span class="line">    &#125;</span><br><span class="line">      </span><br><span class="line">    public @type@  get@Uproperty@()&#123;</span><br><span class="line">        return this.@property@;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="11-练习"><a href="#11-练习" class="headerlink" title="11. 练习"></a>11. 练习</h2><h3 id="11-1-复制文件"><a href="#11-1-复制文件" class="headerlink" title="11.1 复制文件"></a>11.1 复制文件</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">package io.demo11;</span><br><span class="line"></span><br><span class="line">// 复制文件是常见的IO操作，设计方法，实现复制源文件srcFile到destFile</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice01 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        File srcFile = new File(&quot;D:\\a.txt&quot;);</span><br><span class="line">        File destFile = new File(&quot;D:\\b.txt&quot;);</span><br><span class="line">        copyFile(srcFile, destFile);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void copyFile(File srcFile, File destFile) &#123;</span><br><span class="line">        try (// 缓存输入流</span><br><span class="line">             BufferedReader br = new BufferedReader(new FileReader(srcFile));</span><br><span class="line">             // 缓存输出流</span><br><span class="line">             PrintWriter pw = new PrintWriter(new FileWriter(destFile))) &#123;</span><br><span class="line">            String line;</span><br><span class="line">            while (null != (line = br.readLine())) &#123;</span><br><span class="line">                pw.println(line);</span><br><span class="line">            &#125;</span><br><span class="line">            pw.flush();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; finally &#123;</span><br><span class="line">            System.out.println(&quot;文件复制完成&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="11-2-复制文件夹"><a href="#11-2-复制文件夹" class="headerlink" title="11.2 复制文件夹"></a>11.2 复制文件夹</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">package io.demo11;</span><br><span class="line"></span><br><span class="line">// 复制文件夹，把源文件夹下所有的文件，复制到目标文件夹下（包括子文件夹）</span><br><span class="line"></span><br><span class="line">// 流程：使用递归遍历文件夹，遇到文件调用复制文件方法，并获取文件的父目录，在目标文件夹下创建对应的父目录</span><br><span class="line"></span><br><span class="line">import java.io.*;</span><br><span class="line"></span><br><span class="line">public class Practice02 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        String srcFolder = &quot;D:/MyBlog&quot;;</span><br><span class="line">        String destFolder = &quot;D:/a&quot;;</span><br><span class="line">        copyFolder(srcFolder, destFolder);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void copyFolder(String srcFolder, String destFolder) &#123;</span><br><span class="line">        String srcFolderName = srcFolder.split(&quot;/&quot;)[1];</span><br><span class="line">        File src = new File(srcFolder);</span><br><span class="line">        goThroughDirectoryAndCopyFile(src, srcFolderName, destFolder);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void goThroughDirectoryAndCopyFile(File srcFolder, String srcFolderName,</span><br><span class="line">                                                     String destFolder) &#123;</span><br><span class="line">        for (File file : srcFolder.listFiles()) &#123;</span><br><span class="line">            if (file.isDirectory()) &#123;</span><br><span class="line">                goThroughDirectoryAndCopyFile(file, srcFolderName, destFolder);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                System.out.println(&quot;文件名：&quot; + file.getName() +</span><br><span class="line">                        &quot;, 大小为&quot; + file.length() + &quot;字节&quot;);</span><br><span class="line">                copyFile(file, srcFolderName, destFolder);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void copyFile(File srcFile, String srcFolderName, String destFile) &#123;</span><br><span class="line">        // 获取源文件的目录</span><br><span class="line">        String srcPath = srcFile.getAbsolutePath();</span><br><span class="line">        // 按源文件夹名称进行分割，再合并目标文件夹路径则是目标文件路径</span><br><span class="line">        String destPath = destFile + srcPath.split(srcFolderName)[1];</span><br><span class="line">        File targetFile = new File(destPath);</span><br><span class="line">        if (!targetFile.exists()) &#123;</span><br><span class="line">            targetFile.getParentFile().mkdirs();</span><br><span class="line">        &#125;</span><br><span class="line">        try (BufferedReader br = new BufferedReader(new FileReader(srcFile));</span><br><span class="line">             PrintWriter pw = new PrintWriter(new FileWriter(targetFile))) &#123;</span><br><span class="line">            String line;</span><br><span class="line">            while (null != (line = br.readLine())) &#123;</span><br><span class="line">                pw.println(line);</span><br><span class="line">            &#125;</span><br><span class="line">            pw.flush();</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="11-3-查找文件内容"><a href="#11-3-查找文件内容" class="headerlink" title="11.3 查找文件内容"></a>11.3 查找文件内容</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">package io.demo11;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">查找文件内容，假设你的项目目录是e:/project, 遍历这个目录下所有的java文件(包括子文件夹)</span><br><span class="line">找出文件内容包括某个字符串的那些文件，并打印出来。</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">import java.io.BufferedReader;</span><br><span class="line">import java.io.File;</span><br><span class="line">import java.io.FileReader;</span><br><span class="line">import java.io.IOException;</span><br><span class="line"></span><br><span class="line">public class Practice03 &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        File folder = new File(</span><br><span class="line">                &quot;D:\\Simulation and Calculation Projects\\Java\\BasisKnowledgeCode&quot;);</span><br><span class="line">        String search = &quot;Practice01&quot;;</span><br><span class="line">        search(folder, search);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void search(File folder, String search) &#123;</span><br><span class="line">        goThroughDirectory(folder, search);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void goThroughDirectory(File folder, String search) &#123;</span><br><span class="line">        for (File file : folder.listFiles()) &#123;</span><br><span class="line">            if (file.isDirectory()) &#123;</span><br><span class="line">                goThroughDirectory(file, search);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                // 判断文件是否以.java结尾</span><br><span class="line">                if (file.getAbsolutePath().endsWith(&quot;.java&quot;)) &#123;</span><br><span class="line">                    searchOneFile(file, search);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void searchOneFile(File file, String search) &#123;</span><br><span class="line">        try (BufferedReader br = new BufferedReader(new FileReader(file))) &#123;</span><br><span class="line">            String line;</span><br><span class="line">            while (null != (line = br.readLine())) &#123;</span><br><span class="line">                if (line.contains(search)) &#123;</span><br><span class="line">                    System.out.println(String.format(&quot;找到子字符串%s，在文件：%s&quot;,</span><br><span class="line">                            search, file.getAbsolutePath()));</span><br><span class="line">                    break;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="12-总结"><a href="#12-总结" class="headerlink" title="12. 总结"></a>12. 总结</h2><p><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/%E6%80%BB%E7%BB%93.png" alt=""></p>
<ul>
<li>继承InputStream/OutputStream的，为真正意义上的流</li>
<li>继承Reader/Writer的，是流的处理方式。内部创建了流，解决了编码问题</li>
<li>继承Buffer的，是流的处理方式。使用外部传入的流，解决IO频繁问题。</li>
</ul>
<p>除了这些流之外，还有很多其它流。大体上使用差不多的，只是在一些特殊场合下用起来更加方便，在工作中用到的时候再进行学习就可以了。</p>
<h2 id="13-参考"><a href="#13-参考" class="headerlink" title="13. 参考"></a>13. 参考</h2><ul>
<li><a target="_blank" rel="noopener" href="https://how2j.cn/k/io/io-file/345.html">HOW2J.CN</a></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Shen Shilei</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.shenshilei.site/post/d7ed/">https://blog.shenshilei.site/post/d7ed/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.shenshilei.site" target="_blank">ShenShilei's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/IO/">IO</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/SunlitScree_ZH-CN7556627842_1920x1080.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏<div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/WeChanSQ.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/WeChanSQ.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/AliPayQR.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/AliPayQR.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/ffa9/"><img class="prev-cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/ReconciliationDay_ZH-CN7914130812_1920x1080.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">前端学习-HTML</div></div></a></div><div class="next-post pull-right"><a href="/post/9ee3/"><img class="next-cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/Windhorses_ZH-CN5349922758_1920x1080.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java学习笔记-数字与字符串</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/1750/" title="将Web项目部署在阿里云服务器"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/96.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-08</div><div class="title">将Web项目部署在阿里云服务器</div></div></a></div><div><a href="/post/b1ef/" title="Java学习笔记-JSP"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/FormentorHolidays_ZH-CN3392936755_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-04</div><div class="title">Java学习笔记-JSP</div></div></a></div><div><a href="/post/ca10/" title="Java学习笔记-Tomcat和Servlet"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/PascuaFlorida_ZH-CN7720904158_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-31</div><div class="title">Java学习笔记-Tomcat和Servlet</div></div></a></div><div><a href="/post/9ee3/" title="Java学习笔记-数字与字符串"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/Windhorses_ZH-CN5349922758_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-29</div><div class="title">Java学习笔记-数字与字符串</div></div></a></div><div><a href="/post/3da9/" title="使用QQ音乐Api批量下载音乐"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/115.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-22</div><div class="title">使用QQ音乐Api批量下载音乐</div></div></a></div><div><a href="/post/44e3/" title="设计购物车小项目"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/171.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-03</div><div class="title">设计购物车小项目</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@1.6/img/custom/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Shen Shilei</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">60</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/shenshilei1022"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/shenshilei1022" target="_blank" title="Github"><i class="iconfont icon-github1"></i></a><a class="social-icon" href="mailto:1774822536@qq.com" target="_blank" title="Email"><i class="iconfont icon-youxiang"></i></a><a class="social-icon" href="https://gitee.com/shenshilei1022" target="_blank" title="Gitee"><i class="iconfont icon-github2"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=77600006" target="_blank" title="Netease Music"><i class="iconfont icon-wangyiyunyinle"></i></a><a class="social-icon" href="https://space.bilibili.com/15250718" target="_blank" title="Bilibili"><i class="iconfont icon-bilibili1"></i></a></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">活在当下</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-I-O-%E6%96%87%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="toc-text">1. I&#x2F;O 文件对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E4%BB%80%E4%B9%88%E6%98%AF%E6%B5%81%EF%BC%9F"><span class="toc-text">2. 什么是流？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%AD%97%E8%8A%82%E6%B5%81"><span class="toc-text">3. 字节流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-ASCII%E7%A0%81"><span class="toc-text">3.1 ASCII码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E4%BB%A5%E5%AD%97%E8%8A%82%E6%B5%81%E7%9A%84%E5%BD%A2%E5%BC%8F%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9"><span class="toc-text">3.2 以字节流的形式读取文件内容</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%85%B3%E9%97%AD%E6%B5%81%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="toc-text">4. 关闭流的方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E5%9C%A8try%E4%B8%AD%E5%85%B3%E9%97%AD"><span class="toc-text">4.1 在try中关闭</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E5%9C%A8finally%E4%B8%AD%E5%85%B3%E9%97%AD"><span class="toc-text">4.2 在finally中关闭</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E4%BD%BF%E7%94%A8try-%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="toc-text">4.3 使用try()的方式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E5%AD%97%E7%AC%A6%E6%B5%81"><span class="toc-text">5. 字符流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E4%BD%BF%E7%94%A8%E5%AD%97%E7%AC%A6%E6%B5%81%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6"><span class="toc-text">5.1 使用字符流读取文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AD%97%E7%AC%A6%E6%B5%81%E6%8A%8A%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%99%E5%85%A5%E5%88%B0%E6%96%87%E4%BB%B6"><span class="toc-text">5.2 使用字符流把字符串写入到文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E4%B8%AD%E6%96%87%E9%97%AE%E9%A2%98"><span class="toc-text">6. 中文问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98"><span class="toc-text">6.1 编码问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-2-UNICODE%E5%92%8CUTF"><span class="toc-text">6.2 UNICODE和UTF</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E7%BC%93%E5%AD%98%E6%B5%81"><span class="toc-text">7. 缓存流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1-%E4%BD%BF%E7%94%A8%E7%BC%93%E5%AD%98%E6%B5%81%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-text">7.1 使用缓存流读取数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2-%E4%BD%BF%E7%94%A8%E7%BC%93%E5%AD%98%E6%B5%81%E5%86%99%E5%87%BA%E6%95%B0%E6%8D%AE"><span class="toc-text">7.2 使用缓存流写出数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-3-flush"><span class="toc-text">7.3 flush</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E6%95%B0%E6%8D%AE%E6%B5%81"><span class="toc-text">8. 数据流</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E5%AF%B9%E8%B1%A1%E6%B5%81"><span class="toc-text">9. 对象流</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-System-in"><span class="toc-text">10. System.in</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#10-1-Scanner%E8%AF%BB%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">10.1 Scanner读取字符串</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-%E7%BB%83%E4%B9%A0"><span class="toc-text">11. 练习</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#11-1-%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6"><span class="toc-text">11.1 复制文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-2-%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6%E5%A4%B9"><span class="toc-text">11.2 复制文件夹</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-3-%E6%9F%A5%E6%89%BE%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9"><span class="toc-text">11.3 查找文件内容</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-%E6%80%BB%E7%BB%93"><span class="toc-text">12. 总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-%E5%8F%82%E8%80%83"><span class="toc-text">13. 参考</span></a></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/ceb5/" title="在Deepin上安装Eclipse"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/20210911113215.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在Deepin上安装Eclipse"/></a><div class="content"><a class="title" href="/post/ceb5/" title="在Deepin上安装Eclipse">在Deepin上安装Eclipse</a><time datetime="2021-09-11T02:46:06.000Z" title="发表于 2021-09-11 10:46:06">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/6b0/" title="使用VMware创建CentOS7.6虚拟机"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/centos-motif.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="使用VMware创建CentOS7.6虚拟机"/></a><div class="content"><a class="title" href="/post/6b0/" title="使用VMware创建CentOS7.6虚拟机">使用VMware创建CentOS7.6虚拟机</a><time datetime="2021-03-07T14:56:59.000Z" title="发表于 2021-03-07 22:56:59">2021-03-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/1ea5/" title="前端练习-九宫格"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/353.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前端练习-九宫格"/></a><div class="content"><a class="title" href="/post/1ea5/" title="前端练习-九宫格">前端练习-九宫格</a><time datetime="2020-09-14T09:06:40.000Z" title="发表于 2020-09-14 17:06:40">2020-09-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/7240/" title="LeetCode-组合(每日一题)"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/357.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode-组合(每日一题)"/></a><div class="content"><a class="title" href="/post/7240/" title="LeetCode-组合(每日一题)">LeetCode-组合(每日一题)</a><time datetime="2020-09-08T07:13:45.000Z" title="发表于 2020-09-08 15:13:45">2020-09-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/aeeb/" title="LeetCode-前K个高频元素(每日一题)"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/357.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode-前K个高频元素(每日一题)"/></a><div class="content"><a class="title" href="/post/aeeb/" title="LeetCode-前K个高频元素(每日一题)">LeetCode-前K个高频元素(每日一题)</a><time datetime="2020-09-07T08:46:04.000Z" title="发表于 2020-09-07 16:46:04">2020-09-07</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/SunlitScree_ZH-CN7556627842_1920x1080.jpg)"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Shen Shilei</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">回首向来萧瑟处，归去，也无风雨也无晴。</div><div class="icp"><a><img class="icp-icon" src="/img/icp.png" alt="ICP"/><span>陕ICP备2020013027号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    let initData = {
      el: '#vcomment',
      appId: 'A2SjWuspv5TQF8rzabRx8Y1d-MdYXbMMI',
      appKey: 'SYbn4zrNF0biLCBhwQ0956wm',
      placeholder: '留下你的足迹...(昵称中填写QQ号可使用QQ头像和QQ邮箱)',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: 'https://cdn.jsdelivr.net/gh/shenshilei1022/emotion@1.0/',
      emojiMaps: {"1":"aru/1.png","2":"aru/2.png","3":"aru/3.png","4":"aru/4.png","5":"aru/5.png","6":"aru/6.png","7":"aru/7.png","8":"aru/8.png","9":"aru/9.png","10":"aru/10.png","11":"aru/11.png","12":"aru/12.png","13":"aru/13.png","14":"aru/14.png","15":"aru/15.png","16":"aru/16.png","17":"aru/17.png","18":"aru/18.png","19":"aru/19.png","20":"aru/20.png","21":"aru/21.png","22":"aru/22.png","23":"aru/23.png","24":"aru/24.png","25":"aru/25.png","26":"aru/26.png","27":"aru/27.png","28":"aru/28.png","29":"aru/29.png","30":"aru/30.png","31":"aru/31.png","32":"aru/32.png","33":"aru/33.png","34":"aru/34.png","35":"aru/35.png","36":"aru/36.png","37":"aru/37.png","38":"aru/38.png","39":"猥琐萌/1.jpg","40":"猥琐萌/2.jpg","41":"猥琐萌/3.jpg","42":"猥琐萌/4.jpg","43":"猥琐萌/5.jpg","44":"猥琐萌/6.jpg","45":"猥琐萌/7.jpg","46":"猥琐萌/8.jpg","47":"猥琐萌/9.jpg","48":"猥琐萌/10.jpg","49":"猥琐萌/11.jpg","50":"猥琐萌/12.jpg","51":"猥琐萌/13.jpg","52":"猥琐萌/14.jpg","53":"猥琐萌/15.jpg","54":"猥琐萌/16.jpg","55":"猥琐萌/17.jpg","56":"猥琐萌/18.jpg","57":"猥琐萌/19.jpg","58":"猥琐萌/20.jpg","59":"猥琐萌/21.jpg","60":"猥琐萌/22.jpg","61":"猥琐萌/23.jpg","62":"猥琐萌/24.jpg","63":"猥琐萌/25.jpg","64":"猥琐萌/26.jpg","65":"猥琐萌/27.jpg","66":"猥琐萌/28.jpg","67":"猥琐萌/29.jpg","68":"猥琐萌/30.jpg","69":"猥琐萌/31.jpg","70":"猥琐萌/32.jpg","71":"猥琐萌/33.jpg","72":"猥琐萌/34.jpg","73":"猥琐萌/35.jpg","74":"猥琐萌/36.jpg","75":"猥琐萌/37.jpg","76":"猥琐萌/38.jpg","77":"猥琐萌/39.jpg","78":"猥琐萌/40.jpg","79":"猥琐萌/41.jpg","80":"猥琐萌/42.jpg","81":"猥琐萌/43.jpg","82":"猥琐萌/44.jpg","83":"猥琐萌/45.jpg","84":"猥琐萌/46.jpg","85":"猥琐萌/47.jpg","86":"猥琐萌/48.jpg","87":"猥琐萌/49.jpg","88":"猥琐萌/50.jpg","89":"猥琐萌/51.jpg","90":"猥琐萌/52.jpg","91":"猥琐萌/53.jpg","92":"猥琐萌/54.jpg","93":"猥琐萌/55.jpg","94":"猥琐萌/56.jpg","95":"猥琐萌/57.jpg","96":"猥琐萌/58.jpg","97":"猥琐萌/59.jpg","98":"猥琐萌/60.jpg","djxbz":"copy/djxbz.jpg","gan":"copy/gan.jpg","hp":"copy/hp.jpg","huaiyi":"copy/huaiyi.jpg","imgcao":"copy/imgcao.jpg","imghua":"copy/imghua.jpg","imgri":"copy/imgri.jpg","mudenggoudai":"copy/mudenggoudai.jpg","pc":"copy/pc.jpg","tlspg":"copy/tlspg.jpg","tql":"copy/tql.jpg","wuyu":"copy/wuyu.jpg","zzxbz":"copy/zzxbz.jpg","icon_huaji":"copy/icon_huaji.png","仙女下凡":"copy/仙女下凡.webp","听歌":"copy/听歌.webp","嫌弃":"copy/嫌弃.webp","害羞":"bilibili/tv_害羞.png","彩虹滑稽":"copy/彩虹滑稽.webp","快走":"copy/快走.webp","扯脸":"copy/扯脸.webp","摸脸":"copy/摸脸.webp","流汗":"bilibili/tv_流汗.png","滑滑板":"copy/滑滑板.webp","点赞":"bilibili/tv_点赞.png","转圈圈":"copy/转圈圈.webp","光头小人1":"copy/光头小人_(1).gif","光头小人19":"copy/光头小人_(19).gif","光头小人20":"copy/光头小人_(20).gif","光头小人21":"copy/光头小人_(21).gif","光头小人22":"copy/光头小人_(22).gif","光头小人23":"copy/光头小人_(23).gif","光头小人24":"copy/光头小人_(24).gif","光头小人25":"copy/光头小人_(25).gif","光头小人26":"copy/光头小人_(26).gif","光头小人27":"copy/光头小人_(27).gif","光头小人28":"copy/光头小人_(28).gif","光头小人29":"copy/光头小人_(29).gif","光头小人30":"copy/光头小人_(30).gif","光头小人31":"copy/光头小人_(31).gif","光头小人32":"copy/光头小人_(32).gif","光头小人33":"copy/光头小人_(33).gif","光头小人34":"copy/光头小人_(34).gif","光头小人35":"copy/光头小人_(35).gif","光头小人36":"copy/光头小人_(36).gif","光头小人37":"copy/光头小人_(37).gif","光波":"copy/光波.gif","厕所1":"copy/厕所_(1).gif","厕所2":"copy/厕所_(2).gif","厕所3":"copy/厕所_(3).gif","咸鱼":"copy/咸鱼.gif","奏乐1":"copy/奏乐_(1).gif","奏乐10":"copy/奏乐_(10).gif","奏乐2":"copy/奏乐_(2).gif","奏乐4":"copy/奏乐_(4).gif","奏乐5":"copy/奏乐_(5).gif","奏乐6":"copy/奏乐_(6).gif","奏乐7":"copy/奏乐_(7).gif","奏乐8":"copy/奏乐_(8).gif","奏乐9":"copy/奏乐_(9).gif","奔跑狗5":"copy/奔跑狗 (5).gif","奔跑狗1":"copy/奔跑狗_(1).gif","奔跑狗2":"copy/奔跑狗_(2).gif","奔跑狗3":"copy/奔跑狗_(3).gif","奔跑狗4":"copy/奔跑狗_(4).gif","害怕滑稽":"copy/害怕滑稽.gif","小人儿10":"copy/小人儿_(10).gif","小兔吃东西12":"copy/小兔吃东西_(12).gif","小兔吃东西13":"copy/小兔吃东西_(13).gif","小兔吃东西14":"copy/小兔吃东西_(14).gif","小兔吃东西18":"copy/小兔吃东西_(18).gif","小兔吃东西19":"copy/小兔吃东西_(19).gif","小兔吃东西20":"copy/小兔吃东西_(20).gif","小兔吃东西21":"copy/小兔吃东西_(21).gif","小兔吃东西22":"copy/小兔吃东西_(22).gif","小疯子2":"copy/小疯子_(2).gif","小疯子3":"copy/小疯子_(3).gif","小疯子4":"copy/小疯子_(4).gif","小疯子5":"copy/小疯子_(5).gif","小疯子6":"copy/小疯子_(6).gif","小疯子7":"copy/小疯子_(7).gif","小疯子8":"copy/小疯子_(8).gif","扇子狗":"copy/扇子狗.gif","抓1":"copy/抓_(1).gif","抓2":"copy/抓_(2).gif","掐脖子":"copy/掐脖子.gif","滑稽1":"copy/滑稽_(1).gif","滑稽2":"copy/滑稽_(2).gif","滑稽3":"copy/滑稽_(3).gif","滑稽4":"copy/滑稽_(4).gif","跪坐1":"copy/跪坐_(1).gif","跪坐2":"copy/跪坐_(2).gif","鹦鹉小恐龙1":"copy/鹦鹉小恐龙_(1).gif","鹦鹉小恐龙2":"copy/鹦鹉小恐龙_(2).gif","doge":"bilibili/tv_doge.png","亲亲":"bilibili/tv_亲亲.png","偷笑":"bilibili/tv_偷笑.png","再见":"bilibili/tv_再见.png","冷漠":"bilibili/tv_冷漠.png","发怒":"bilibili/tv_发怒.png","发财":"bilibili/tv_发财.png","可爱":"bilibili/tv_可爱.png","吐血":"bilibili/tv_吐血.png","呆":"bilibili/tv_呆.png","呕吐":"bilibili/tv_呕吐.png","困":"bilibili/tv_困.png","坏笑":"bilibili/tv_坏笑.png","大佬":"bilibili/tv_大佬.png","大哭":"bilibili/tv_大哭.png","委屈":"bilibili/tv_委屈.png","尴尬":"bilibili/tv_尴尬.png","微笑":"bilibili/tv_微笑.png","思考":"bilibili/tv_思考.png","惊吓":"bilibili/tv_惊吓.png","打脸":"bilibili/tv_打脸.png","抓狂":"bilibili/tv_抓狂.png","抠鼻":"bilibili/tv_抠鼻.png","斜眼笑":"bilibili/tv_斜眼笑.png","无奈":"bilibili/tv_无奈.png","晕":"bilibili/tv_晕.png","流泪":"bilibili/tv_流泪.png","流鼻血":"bilibili/tv_流鼻血.png","生气":"bilibili/tv_生气.png","生病":"bilibili/tv_生病.png","疑问":"bilibili/tv_疑问.png","白眼":"bilibili/tv_白眼.png","皱眉":"bilibili/tv_皱眉.png","目瞪口呆":"bilibili/tv_目瞪口呆.png","睡着":"bilibili/tv_睡着.png","笑哭":"bilibili/tv_笑哭.png","腼腆":"bilibili/tv_腼腆.png","色":"bilibili/tv_色.png","调侃":"bilibili/tv_调侃.png","调皮":"bilibili/tv_调皮.png","鄙视":"bilibili/tv_鄙视.png","闭嘴":"bilibili/tv_闭嘴.png","难过":"bilibili/tv_难过.png","馋":"bilibili/tv_馋.png","鬼脸":"bilibili/tv_鬼脸.png","黑人问号":"bilibili/tv_黑人问号.png","鼓掌":"bilibili/tv_鼓掌.png"},
      enableQQ: true,
      path: window.location.pathname,
    }

    if (true) { 
      initData.requiredFields= ('nick,mail'.split(','))
    }
    
    if (false) {
      const otherData = false
      initData = Object.assign({}, initData, otherData)
    }
    
    const valine = new Valine(initData)
  }

  if (typeof Valine === 'function') initValine() 
  else $.getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js', initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.querySelector('#vcomment'),loadValine)
  else setTimeout(() => loadValine(), 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div class="aplayer no-destroy" data-id="4960099490" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="false" muted></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config_change',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/post/a7b7/"]):not([href="/artitalk/"]):not([href="/post/6a69/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  $('script[data-pjax]').each(function () {
    $(this).parent().append($(this).remove())
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  if (typeof gtag === 'function') {
    gtag('config', '', {'page_path': window.location.pathname});
  }

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})


document.addEventListener('pjax:send', function () {
  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  $(window).off('scroll')

  //reset readmode
  $('body').hasClass('read-mode') && $('body').removeClass('read-mode')

})</script></div></body></html>